package com.xauto.pioneer.appuser.adapter;

import android.app.Activity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.model.ParkingSite;

/**
 * Created by Lemon on 2015/12/11.
 * Desc:取车网点列表 适配器
 */
public class ParkingSpaceAdapter extends AbsAdapter<ParkingSite> {
    public ParkingSpaceAdapter(Activity context, int layout) {
        super(context, layout);
    }

    @Override
    public ViewHolder<ParkingSite> getHolder() {
        return new TemplateViewHolder();
    }


    private class TemplateViewHolder implements ViewHolder<ParkingSite> {

        private TextView tvParkingSiteName;
        private TextView tvParkingSiteAddress;
        private TextView tvParkingSiteDistance;
        private TextView tvParkingSiteValu;
        @Override
        public void initViews(View v, int position) {
            tvParkingSiteName = (TextView) v.findViewById(R.id.tvParkingSiteName);
            tvParkingSiteAddress = (TextView) v.findViewById(R.id.tvParkingSiteAddress);
            tvParkingSiteDistance = (TextView) v.findViewById(R.id.tvParkingSiteDistance);
            tvParkingSiteValu= (TextView) v.findViewById(R.id.tvParkingSiteValu);
        }

        @Override
        public void updateData(ParkingSite parkingSite, int position) {
            tvParkingSiteName.setText(parkingSite.parking_site_name);
//            tvParkingSiteAddress.setText(parkingSite.address);
            tvParkingSiteDistance.setText(parkingSite.distance_format);
            tvParkingSiteValu.setText(parkingSite.distance_format);
            tvParkingSiteValu.setText(context.getString(R.string.park_return_value,parkingSite.enter_expense));
            //根据可用车辆是否未一辆区分颜色显示
            String content = parkingSite.address+"("+parkingSite.usable_space_count+"车位)";
            SpannableStringBuilder builder = new SpannableStringBuilder(content);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.main_gray));
            ForegroundColorSpan whiteSpan = new ForegroundColorSpan(parkingSite.usable_space_count == 0 ? context.getResources().getColor(R.color.main_gray) : context.getResources().getColor(R.color.main_yellow));
            builder.setSpan(redSpan, 0, content.indexOf("("), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(whiteSpan,content.indexOf("("), content.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            tvParkingSiteAddress.setText(builder);
        }

        @Override
        public void doOthers(ParkingSite parkingSite, int position) {

        }
    }
}
